package com.mock.action.contact;

import java.util.ArrayList;
import java.util.List;

import com.mock.dao.BestContactMethodDao;
import com.mock.dao.ContactDao;
import com.mock.dao.ContactTypeDao;
import com.mock.dao.impl.BestContactMethodDaoImpl;
import com.mock.dao.impl.ContactDaoImpl;
import com.mock.dao.impl.ContactTypeDaoImpl;
import com.mock.model.BestContactMethod;
import com.mock.model.Contact;
import com.mock.model.ContactType;
import com.opensymphony.xwork2.ActionSupport;

public class EditContactAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String firstnameField;
	private String surnameField;
	private String knownAsField;
	private String officePhoneField;
	private String mobilePhoneField;
	private String stHomePhoneField;
	private String emailAddressField;
	private long managerNameField;
	private String managerNameFieldFake;
	private long contactType;
	private long bestContactMethod;
	private String jobRoleField;
	private String workbaseField;
	private String jobTitleField;
	private String check;
	private long contactIdField;

	private List<Contact> contacts;
	private Contact contact;
	private Contact manager;
	private List<ContactType> contactTypes;

	private List<BestContactMethod> bestContactMethods;

	private ContactDao contactDao = new ContactDaoImpl();
	private ContactTypeDao contactTypeDao = new ContactTypeDaoImpl();
	private BestContactMethodDao bestContactMethodDao = new BestContactMethodDaoImpl();

	long id = -1L;

	@Override
	public String execute() throws Exception {
		generateData();
		return SUCCESS;
	}

	public void generateData() {
		contactTypes = new ArrayList<ContactType>();
		contactTypes = contactTypeDao.getAllContactType();

		bestContactMethods = new ArrayList<BestContactMethod>();
		bestContactMethods = bestContactMethodDao.getAllBestContactMethod();
	}

	public String update() {
		boolean active = false;
		if ("on".equalsIgnoreCase(check)) {
			active = true;
		}
		contact = contactDao.getContactById(contactIdField);
		contact.setFirstname(firstnameField);
		contact.setSurname(surnameField);
		contact.setKnownAs(knownAsField);
		contact.setOfficePhone(officePhoneField);
		contact.setMobilePhone(mobilePhoneField);
		contact.setHomePhone(stHomePhoneField);
		contact.setEmail(emailAddressField);
		contact.setManager(managerNameField);
		contact.setContactType(contactTypeDao.getContactTypeById(contactType));
		contact.setBestContactMethod(bestContactMethodDao
				.getBestContactMethodById(bestContactMethod));
		contact.setJobRole(jobRoleField);
		contact.setWorkbase(workbaseField);
		contact.setJobTitle(jobTitleField);
		contact.setActive(active);
		contactDao.update(contact);
		return SUCCESS;
	}

	public String getFirstnameField() {
		return firstnameField;
	}

	public void setFirstnameField(String firstnameField) {
		this.firstnameField = firstnameField;
	}

	public String getSurnameField() {
		return surnameField;
	}

	public void setSurnameField(String surnameField) {
		this.surnameField = surnameField;
	}

	public String getKnownAsField() {
		return knownAsField;
	}

	public void setKnownAsField(String knownAsField) {
		this.knownAsField = knownAsField;
	}

	public String getOfficePhoneField() {
		return officePhoneField;
	}

	public void setOfficePhoneField(String officePhoneField) {
		this.officePhoneField = officePhoneField;
	}

	public String getMobilePhoneField() {
		return mobilePhoneField;
	}

	public void setMobilePhoneField(String mobilePhoneField) {
		this.mobilePhoneField = mobilePhoneField;
	}

	public String getStHomePhoneField() {
		return stHomePhoneField;
	}

	public void setStHomePhoneField(String stHomePhoneField) {
		this.stHomePhoneField = stHomePhoneField;
	}

	public String getEmailAddressField() {
		return emailAddressField;
	}

	public void setEmailAddressField(String emailAddressField) {
		this.emailAddressField = emailAddressField;
	}

	public long getManagerNameField() {
		managerNameField = manager.getId();
		return managerNameField;
	}

	public void setManagerNameField(long managerNameField) {
		this.managerNameField = managerNameField;
	}

	public String getManagerNameFieldFake() {
		managerNameFieldFake = manager.getFirstname() + " "
				+ manager.getSurname();
		return managerNameFieldFake;
	}

	public void setManagerNameFieldFake(String managerNameFieldFake) {
		this.managerNameFieldFake = managerNameFieldFake;
	}

	public long getContactType() {
		return contactType;
	}

	public void setContactType(long contactType) {
		this.contactType = contactType;
	}

	public long getBestContactMethod() {
		return bestContactMethod;
	}

	public void setBestContactMethod(long bestContactMethod) {
		this.bestContactMethod = bestContactMethod;
	}

	public String getJobRoleField() {
		return jobRoleField;
	}

	public void setJobRoleField(String jobRoleField) {
		this.jobRoleField = jobRoleField;
	}

	public String getWorkbaseField() {
		return workbaseField;
	}

	public void setWorkbaseField(String workbaseField) {
		this.workbaseField = workbaseField;
	}

	public String getJobTitleField() {
		return jobTitleField;
	}

	public void setJobTitleField(String jobTitleField) {
		this.jobTitleField = jobTitleField;
	}

	public String getCheck() {
		return check;
	}

	public void setCheck(String check) {
		this.check = check;
	}

	public long getContactIdField() {
		return contactIdField;
	}

	public void setContactIdField(long contactIdField) {
		this.contactIdField = contactIdField;
	}

	public List<Contact> getContacts() {

		return contacts;
	}

	public void setContacts(List<Contact> contacts) {
		this.contacts = contacts;
	}

	public Contact getContact() {
		if (id != -1L) {
			contact = new ContactDaoImpl().getContactById(id);
		}
		return contact;
	}

	public void setContact(Contact contact) {
		this.contact = contact;
	}

	public Contact getManager() {
		manager = new ContactDaoImpl().getContactById(contact.getManager());
		return manager;
	}

	public void setManager(Contact manager) {
		this.manager = manager;
	}

	public List<ContactType> getContactTypes() {
		return contactTypes;
	}

	public void setContactTypes(List<ContactType> contactTypes) {
		this.contactTypes = contactTypes;
	}

	public List<BestContactMethod> getBestContactMethods() {
		return bestContactMethods;
	}

	public void setBestContactMethods(List<BestContactMethod> bestContactMethods) {
		this.bestContactMethods = bestContactMethods;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

}
